﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace GPI.Server
{
	/// <summary>
	/// AccessService
	/// </summary>
	public partial class AccessService
	{
		private readonly DAL.AccessDAL dal=new DAL.AccessDAL();
		public AccessService()
		{}
		#region  Method
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			return dal.Exists(ID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Model.AccessInfo model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.AccessInfo model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			return dal.Delete(ID);
		}
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteFromStr(int id)
        {
            return dal.DeleteFromStr(" roleID="+id);
        }

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			return dal.DeleteList(IDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.AccessInfo GetModel(int ID)
		{
			
			return dal.GetModel(ID);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Model.AccessInfo GetModelByCache(int ID)
		{
			
			string CacheKey = "AccessServiceModel-" + ID;
			object objModel = Moon.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(ID);
					if (objModel != null)
					{
                        int ModelCache = Moon.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Moon.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Model.AccessInfo)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Model.AccessInfo> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Model.AccessInfo> DataTableToList(DataTable dt)
		{
			List<Model.AccessInfo> modelList = new List<Model.AccessInfo>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Model.AccessInfo model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Model.AccessInfo();
					if(dt.Rows[n]["ID"]!=null && dt.Rows[n]["ID"].ToString()!="")
					{
						model.ID=int.Parse(dt.Rows[n]["ID"].ToString());
					}
					if(dt.Rows[n]["roleID"]!=null && dt.Rows[n]["roleID"].ToString()!="")
					{
						model.roleID=int.Parse(dt.Rows[n]["roleID"].ToString());
					}
					if(dt.Rows[n]["moduleID"]!=null && dt.Rows[n]["moduleID"].ToString()!="")
					{
						model.moduleID=int.Parse(dt.Rows[n]["moduleID"].ToString());
					}
					if(dt.Rows[n]["accessType"]!=null && dt.Rows[n]["accessType"].ToString()!="")
					{
						model.accessType=int.Parse(dt.Rows[n]["accessType"].ToString());
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string[] orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		
    }
    #endregion  Method
}
